/*
 * Copyright 2011 www.pretty-tools.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "StdAfx.h"
#include "ResourceResolver.h"

HRESULT ResourceResolver::ResolveURL(const CStringW &strURL, CStringW& strResult)
{
    strResult = strURL;

    CString strProtocol = GetProtocol(strURL);
    if (strProtocol == _T("chrome") )
    {
        RETURN_FAILED(ResolveChrome(strResult))
    }
    else
    {
        RETURN_FAILED(ReplaceGUID(strResult))
    }

    return S_OK;
}

HRESULT ResourceResolver::ResolveChrome(CStringW& strResult)
{
    int idx = strResult.Find( _T('/'), 10);
    if (idx == -1)
        return E_FAIL;

    CString strModulePath;
    RETURN_FAILED(GetModulePath(strResult.Mid(9, idx - 9), strModulePath));

    if ((idx = strResult.ReverseFind( _T('/') )) == -1)
        return E_FAIL;

    CString strResource = strResult.Mid(idx).MakeUpper();

    strResult = (CStringW)L"res://" + strModulePath + strResource;

    return S_OK;
}

HRESULT ResourceResolver::ReplaceGUID(CString& strResult)
{
    int idx = strResult.Find( _T('{') ), iend = strResult.ReverseFind( _T('}') );
    if (idx == -1 && iend == -1)
        return S_OK;

    if (idx == -1 || iend == -1)
        return E_FAIL;

    CString guidModule = strResult.Mid(idx, iend - idx + 1);

    CString strModulePath;
    RETURN_FAILED(GetModulePath(guidModule, strModulePath))

    strResult.Replace(guidModule, strModulePath);
    return S_OK;
}

HRESULT ResourceResolver::GetModulePath(const CString &strModule, CString& strModulePath)
{
    RegKey key;

	CString strKeyRoot;

	if (IsOSVistaOrHigher())
		strKeyRoot = _T("Software\\Microsoft\\Internet Explorer\\LowRegistry\\") _PROJECT_REGKEY _T("\\Extensions\\");
	else
		strKeyRoot = _T("Software\\") _PROJECT_REGKEY _T("\\Extensions\\");

    RETURN_E_FAIL_ON_FALSE(key.OpenEx(HKEY_CURRENT_USER, strKeyRoot + strModule))
    RETURN_HRESULT(key.QueryValue( NULL, strModulePath ));
}

CString ResourceResolver::GetProtocol(const CString &strURL)
{
    int idx = strURL.Find( _T("://") );
    if (idx == -1)
        return CString();

    return strURL.Left(idx).MakeLower();
}

CString ResourceResolver::GetModule(const CString &strURL)
{
    if (GetProtocol(strURL) != _T("res"))
        return CString();

    int idx = strURL.ReverseFind( _T('/') );
    return idx != -1 ? strURL.Mid(6, idx - 6) : strURL.Mid(6);
}

CString ResourceResolver::GetResourceName(const CString &strURL)
{
    int idx = strURL.ReverseFind( _T('/') );
    if (GetProtocol(strURL) != _T("res") || idx == -1)
        return CString();

    return strURL.Mid(idx + 1).MakeUpper();
}
